package com.tanker.util;

import org.apache.commons.lang.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间工具类
 */
public class DateUtils extends org.apache.commons.lang.time.DateUtils {

	private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

	public static final String DATE_DEFAUTE = "yyyyMMddHHmmss";
	public static final String DATE_TIME = "yyyyMMddHHmm";
	public static final String DATE_DEFAUTE_1 = "yyyy-MM-dd HH:mm";
	public static final String DATA_TIME_FORMAT_WITH_T = "yyyyMMdd'T'HHmmss";
	public static final String YEAR_FORMAT = "yyyy";
	public static final String TIME_DEFAUTE="HHmmss";
	public static final String YAER_DATE_DEFAUTE="yyyyMMdd";
	public static final String YAER_DATE_DEFAUTE1="yyyy.MM.dd";
	public static final String YAER_DATE_DEFAUTE2="yyyy-MM-dd";
	/**
	 * 字符串转时间，字符串格式：yyyy-MM-dd HH:mm
	 * @param str
	 * @return
	 */
	public static Date parseDate(String str) {
		return parseDate(str, DATE_DEFAUTE_1);
	}

	public static LocalDateTime parseDate1(String str,String format){
		return LocalDateTime.parse(str, DateTimeFormatter.ofPattern(format));
	}
	/**
	 * 字符串转时间
	 * @param str
	 * @param format
	 * @return
	 */
	public static Date parseDate(String str, String format) {
		try {
			return parseDate(str, new String[]{format});
		} catch (ParseException e) {
			logger.error("parse date error.", e);
		}
		return null;
	}

	/**
	 * 字符串转时间
	 * @param str
	 * @param format
	 * @return
	 */
	public static LocalDateTime parseLocalDateTime(String str, String format){
		return LocalDateTime.parse(str,DateTimeFormatter.ofPattern(format));
	}
	/**
	 * 时间转换成制定的格式
	 * @param date
	 * @param para
	 * @return
	 */
	public static String formate(Date date, String format) {
		return DateFormatUtils.format(date, format);
	}

	/**
	 * 对yyyMMddHHmmss格式的时间加减天得到指定格式的字符串
	 * @param time
	 * @param day
	 * @param format
	 * @return
	 */
	public static String addDay(long time,int day,String format){
		try {
			Calendar  calendar = new  GregorianCalendar();
			Date date = CommonConstants.DATETIME_SEC_STR_PARSE(time+"");
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, day);
			date = calendar.getTime();   //这个时间就是日期往后推一天的结果
			return formate(date,format);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 对yyyMMddHHmmss格式的时间加减天得到指定格式的字符串
	 * @param time
	 * @param day
	 * @param format
	 * @return
	 */
	public static String addDay(Date time,int day,String format){
		Calendar  calendar = new  GregorianCalendar();
		calendar.setTime(time);
		calendar.add(Calendar.DAY_OF_MONTH, day);
		time = calendar.getTime();   //这个时间就是日期往后推一天的结果
		return formate(time,format);
	}
	public static Date addDay(Date time,int day){
		Calendar  calendar = new  GregorianCalendar();
		calendar.setTime(time);
		calendar.add(Calendar.DAY_OF_MONTH, day);
		time = calendar.getTime();   //这个时间就是日期往后推一天的结果
		return time;
	}
	public static void main(String[] args) {
		System.out.println(DateUtils.addDay(new Date(), 8, "yyyy年MM月dd日"));
	}
}
